package design.pattern.Lu06ProxyPattern;

/**
 * @program JavaBase
 * @description: 普通代理模式
 *  - 客户端只能访问代理角色，不能访问真实角色，客户端需要知道代理的存在
 * @author: zhanglu
 * @create: 2020-03-29 10:19:00
 */
public class Lu06ProxyPattern01 {

    public static void main(String[] args) throws Exception {

        GamePlayerProxy01 gamePlayerProxy01 = new GamePlayerProxy01("张三");

        gamePlayerProxy01.login("zhangsan", "111111");
        gamePlayerProxy01.killBoss();
        gamePlayerProxy01.upgrade();

    }

}

interface IGamePlayer01{

    void login(String username, String password);

    void killBoss();

    void upgrade();

}

class GamePlayer01 implements IGamePlayer01{

    private String name;

    public GamePlayer01(IGamePlayer01 iGamePlayer01, String name) throws Exception {
        if(iGamePlayer01 == null){
            throw new Exception("异常");
        }
        this.name = name;
    }

    @Override
    public void login(String username, String password) {
        System.out.println("登陆：" + this.name + " - " + username + " - " + password);
    }

    @Override
    public void killBoss() {
        System.out.println("杀怪：" + this.name);
    }

    @Override
    public void upgrade() {
        System.out.println("升级：" + this.name);
    }

}

class GamePlayerProxy01 implements IGamePlayer01{

    private IGamePlayer01 iGamePlayer01;

    public GamePlayerProxy01(String name) throws Exception {
        iGamePlayer01 = new GamePlayer01(this, name);
    }

    @Override
    public void login(String username, String password) {
        this.iGamePlayer01.login(username, password);
    }

    @Override
    public void killBoss() {
        this.iGamePlayer01.killBoss();
    }

    @Override
    public void upgrade() {
        this.iGamePlayer01.upgrade();
    }
}
